<template>
	<view class="order">
		<view v-if="login">
			<view v-for="(orderdetail,index) in order_list" :key="index">
				<view class="bg-white margin-sm padding-sm solids-bottom shadow radius text-content">
					<view class="flex justify-between">
						<view class="text-bold" style="flex-shrink: 0;">
							<text class="text-red">已定票</text>
						</view>
						<view><text class="icon-goods padding-right-sm"></text><text class="text-bold">{{ orderdetail.orderId }}</text></view>
					</view>
					<view>
						<view class="bg-white margin-xs padding-xs shadow radius text-content">
							<view class="padding-top-sm solids-top">
								<view class="padding-left">{{orderdetail.startRegion}}<text class=" myIcon-jiantou padding-lr-xs"></text>
									{{orderdetail.endRegion}}乘车</view>
								<view class="flex ">
									<view class="flex text-content padding-left justify-between" style="flex-grow: 1;">
										<view>{{orderdetail.speedDate}} {{orderdetail.speedTime}}乘车</view>
										<view style="flex-shrink: 0;">
											<view>￥{{ orderdetail.speedMon }}</view>
											<view class="text-grey text-center">×{{orderdetail.pollNum}}</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="flex justify-end margin-tb-sm">共{{orderdetail.pollNum}}件商品，合计{{ orderdetail.allPrice }}元</view>
					<view class="flex justify-end margin-tb-sm" style="font-size: 24upx;">
						<view v-if="orderdetail.showCancel">
							<view class="round cu-btn lines-grey mid shadow margin-right-sm" @click.stop="changeSpeed(orderdetail)">更换座位</view>
							<view class="round cu-btn lines-orange mid shadow" @click.stop="cancel(orderdetail.orderId)">取消订单</view>
						</view>
					</view>
				</view>
			</view>
			<view class="cu-load" style="color: #CCCCCC;" :class="!isLoad?'loading':'over'"></view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				limit: 3,
				order_list: [],
				login: false,
				isLoad: false
			}
		},
		onShow() {
			if (!this.$db.get('token')) {
				this.$comm.errorToShow('请先登录')
			} else {
				this.login = true
				this.page = 1
				this.limit = 3
				this.order_list = []
				this.getOrderList()
			}
		},
		methods: {
			getOrderList() {
				this.$api.getOrder({
					page: this.page,
					limit: this.limit
				}).then(res => {
					if (res.code) {
						if (this.order_list.length > 0) {

							this.order_list = this.order_list.concat(res.data)
						} else {
							this.order_list = res.data
						}
						if (res.data.length < this.limit) {
							this.isLoad = true
						}
						this.page++
					}
				})
			},
			cancel(orderId) {
				wx.showModal({
					title: '提示',
					content: '确认删除该订单?',
					success: (res) => {
						if (res.confirm) {
							this.$api.delOrder({
								orderId
							}).then(res => {
								if (res.code) {
									this.$comm.successToShow('删除成功')
									let orderIndex = this.order_list.findIndex(order => order.orderId == orderId)
									orderIndex != -1 && this.order_list.splice(orderIndex, 1)
								}
							})
						}
					}
				})
			},
			changeSpeed(orderdetail) {
				uni.$once('changeSpeed',(res)=>{
					this.$api.updataOrder(res).then(res=>{
						if(res.code){
							this.$comm.successToShow('更换座位成功')
						}
					})
				})
				this.$comm.navigateTo("/pages/changeDetail/changeDetail?orderdetail=" + JSON.stringify(orderdetail))
			}
		},
		onReachBottom() {
			if (!this.isLoad) {
				this.getOrderList()
			}
		}
	}
</script>

<style>
	.cover {
		width: 150upx;
		height: 150upx;
	}
</style>
